package com.wy.wait_and_notify;

import java.util.concurrent.TimeUnit;

/**
 * @author 美女
 * @date 2022/05/02 15:04
 **/
public class Demo3 {
    static Object o=new Object();
    static class MyThread extends Thread{
        @Override
        public void run() {
            synchronized (o){
                try {
                    o.wait();
                    System.out.println(getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            MyThread t=new MyThread();
            t.start();
        }
        //启动了10个线程
        //保证子线程们先wait，主线程就先休眠一会
        TimeUnit.SECONDS.sleep(5);
        synchronized (o){
            o.notify();//随机唤醒一个子线程
//            o.notifyAll();//唤醒所有子线程
        }
    }
}